package markdown

import (
	"fmt"
	"strings"
)

// 请求参数解析

type RequestParameter struct {
	Required    bool   `json:"required,omitempty"`
	Description string `json:"description,omitempty"`
	Ref         string `json:"$ref,omitempty"`
	// Parameter --> parameters
	Name            string             `json:"name,omitempty"`
	In              string             `json:"in,omitempty"`
	Schema          *SchemaOrItems     `json:"schema,omitempty"`
	AllowEmptyValue bool               `json:"allowEmptyValue,omitempty"`
	Style           string             `json:"style,omitempty"`
	Explode         bool               `json:"explode,omitempty"`
	AllowReserved   bool               `json:"allowReserved,omitempty"`
	Example         interface{}        `json:"example,omitempty"`
	Examples        map[string]Example `json:"examples,omitempty"`
	Deprecated      bool               `json:"deprecated,omitempty"`
	// RequestBody --> requestBody
	Content map[string]MediaType `json:"content,omitempty"`
}

func (parameter *RequestParameter) requestParam(resolver *resolver) (reqParamTable, reqParamDesc string) {
	parser := ParamParser{
		ParamTypePosition: ParamTypePosition(parameter.In),
		resolver:          resolver,
	}
	// 组织必填参数项
	required := make([]string, 0)
	if parameter.Required {
		parameter.Required = false
		required = append(required, parameter.Name)
	}
	// 创建新的参数
	schema := new(SchemaOrItems)
	deserialization(parameter, schema)
	paramInfo := parser.newParam(parameter.Name, schema, required)
	// 参数表格
	reqParamTable = strings.Join(paramInfo.TableRow, "")
	switch parameter.In {
	case "body": // body 参数
		// 解析body参数
		parser.ParamTypePosition = BodyParam
		bodyParamInfo := parser.newParam("", parameter.Schema, required)
		mdText := fmt.Sprintf(templateReqParamTable, bodyParamInfo.TableRow)
		// body参数示例
		exText := serialization(bodyParamInfo.Value)
		reqParamDesc = fmt.Sprintf(templateExPost, parameter.Name, fmt.Sprintf(templateExample, "", exText), mdText)
	case "header":
	case "formData":
	}

	return reqParamTable, reqParamDesc
}

func (parameter *RequestParameter) requestBody(resolver *resolver) (reqParamDesc string) {
	parser := ParamParser{
		ParamTypePosition: BodyParam,
		resolver:          resolver,
	}
	var paramName = "RequestBody"
	// 组织必填参数项
	required := make([]string, 0)
	if parameter.Required {
		required = append(required, paramName)
	}
	for mediaType, content := range parameter.Content {
		if "application/json" == mediaType {
			parser.MediaType = mediaType
			bodyParamInfo := parser.newParam("", content.Schema, required)
			mdText := fmt.Sprintf(templateReqParamTable, strings.Join(bodyParamInfo.TableRow, ""))
			// body参数示例
			var exampleText = ""
			exText := serialization(bodyParamInfo.Value)
			exampleText = fmt.Sprintf(templateExample, "", exText)
			if content.Example != nil {
				exText = serialization(content.Example)
				exampleText = fmt.Sprintf(templateExample, "", exText)
			}
			if content.Examples != nil {
				exampleText = ""
				for examplesType, example := range content.Examples {
					var exText = ""
					if example.Ref != "" {
						refDefinitionName := example.Ref[strings.LastIndex(example.Ref, "/")+1:]
						refDefinition, exist := resolver.components.Examples[refDefinitionName]
						if exist {
							exText = serialization(refDefinition.Value)
						}
					} else {
						exText = serialization(example.Value)
					}
					exampleText += fmt.Sprintf(templateExample, fmt.Sprintf("- %s %s\n", examplesType, example.Description), exText)
				}
			}
			reqParamDesc += fmt.Sprintf(templateExPost, paramName, fmt.Sprintf("\n- `%s`\n%s", mediaType, exampleText), mdText)
		}
	}
	return reqParamDesc
}
